package fashionshop.gui.main.manage;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.commons.lang3.StringUtils;

import fashionshop.domain.Store;
import fashionshop.gui.common.component.CallbackDialog;
import fashionshop.gui.common.event.DialogSubmitListener;

@SuppressWarnings("serial")
public class AddOrEditStoreDialog extends CallbackDialog<Store> {
	
	private static AddOrEditStoreDialog instance;

	private final JPanel contentPanel = new JPanel();
	private JLabel lblName;
	private JLabel lblAddress;
	private JLabel lblPhone;
	private JTextField txtName;
	private JTextField txtAddress;
	private JTextField txtPhone;
	
	public static AddOrEditStoreDialog getInstance(String title, DialogSubmitListener<Store> listener){
		if(instance == null){
			instance = new AddOrEditStoreDialog();
		}
		instance.setTitle(title);
		instance.listener = listener;
		return instance;
	}

	/**
	 * Create the dialog.
	 */
	private AddOrEditStoreDialog() {
		initComponents();
	}
	private void initComponents() {
		setResizable(false);
		setBounds(100, 100, 450, 208);
		getContentPane().setLayout(new BorderLayout());
		this.contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(this.contentPanel, BorderLayout.CENTER);
		
		this.lblName = new JLabel("Tên cửa hàng:");
		
		this.lblAddress = new JLabel("Địa chỉ:");
		
		this.lblPhone = new JLabel("Số điện thoại:");
		
		this.txtName = new JTextField();
		this.txtName.setColumns(10);
		
		this.txtAddress = new JTextField();
		this.txtAddress.setColumns(10);
		
		this.txtPhone = new JTextField();
		this.txtPhone.setColumns(10);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(this.lblName)
						.addComponent(this.lblAddress)
						.addComponent(this.lblPhone))
					.addGap(26)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(this.txtName, GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
						.addComponent(this.txtAddress, GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
						.addComponent(this.txtPhone, GroupLayout.PREFERRED_SIZE, 164, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(19)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(this.lblName)
						.addComponent(this.txtName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(this.lblAddress)
						.addComponent(this.txtAddress, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(this.lblPhone)
						.addComponent(this.txtPhone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(167, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						submit();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	@Override
	public void populateData(Store entity) {
		entity.setName(StringUtils.trim(txtName.getText()));
		entity.setAddress(StringUtils.trim(txtAddress.getText()));
		entity.setPhone(StringUtils.trim(txtPhone.getText()));
	}

	@Override
	public void updateForm(Store entity) {
		if(entity != null){
			txtName.setText(entity.getName());
			txtAddress.setText(entity.getAddress());
			txtPhone.setText(entity.getPhone());
		}else{
			txtName.setText("");
			txtAddress.setText("");
			txtPhone.setText("");
		}
	}

	@Override
	public List<String> validateForm() {
		List<String> rs = new ArrayList<String>();
		if(txtName.getText() == null || StringUtils.trim(txtName.getText()).length() == 0){
			rs.add("Nhập tên cửa hàng.");
		}else if(StringUtils.trim(txtName.getText()).length() > 100){
			rs.add("Tên cửa hàng không quá 100 ký tự.");
		}
		
		if(txtAddress.getText() == null || StringUtils.trim(txtAddress.getText()).length() == 0){
			rs.add("Nhập địa chỉ.");
		}else if(StringUtils.trim(txtAddress.getText()).length() > 150){
			rs.add("Địa chỉ không quá 150 ký tự.");
		}
		
		if(txtPhone.getText() == null || StringUtils.trim(txtPhone.getText()).length() == 0){
			rs.add("Nhập số điện thoại.");
		}else if(StringUtils.trim(txtPhone.getText()).length() > 20){
			rs.add("Số điện thoại không quá 20 ký tự");
		}
		return rs.size() > 0 ? rs : null;
	}
}
